/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package library.gui;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListCellRenderer;
import library.InventoryChangeListener;
import library.Item;
import library.Library;

/**
 *
 * @author ryan
 */
public class InventoryPanel extends JPanel{
    
    private JList inventoryItems;
    private final Library l;
    private DefaultListModel m;
    private JFrame parent;
    private boolean overdueOnly=false;
    private final SimpleDateFormat df = new SimpleDateFormat("MM/dd/yy");
    public InventoryPanel(Library l){
        this.l=l;
        this.parent=parent;
        init();
    }
    
    private void init(){
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        
        JPanel topInventoryPanel = new JPanel();        
        topInventoryPanel.setLayout(new BoxLayout(topInventoryPanel, BoxLayout.X_AXIS));        
        topInventoryPanel.setMaximumSize(new Dimension(300, 20));
        
        final JTextField dateField = new JTextField(df.format(new Date(System.currentTimeMillis())));
        dateField.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                try{
                    Date date = df.parse(dateField.getText());
                    l.setDate(date);
                }catch(Exception ex){
                    JOptionPane.showMessageDialog(null,"invalid date format must be MM/dd/yy","Date error", JOptionPane.ERROR_MESSAGE); 
                }
            }
        });
        
        topInventoryPanel.add(dateField);
        JButton addItem = new JButton("Add Item");
        
        addItem.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                AddItemPopup addUser = new AddItemPopup(InventoryPanel.this);
                addUser.setVisible(true);
            }        
        });
        
        
        topInventoryPanel.add(addItem);
        
        final JCheckBox overdueOnlyCheckbox = new JCheckBox();
        overdueOnlyCheckbox.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                overdueOnly=overdueOnlyCheckbox.isSelected();
                l.fireUpdateListeners();
            }
        });
        
        JLabel overdueOnlyLbl = new JLabel("Overdue Only");
        topInventoryPanel.add(overdueOnlyLbl);
        topInventoryPanel.add(overdueOnlyCheckbox);
        
        add(topInventoryPanel);
        /*
        JComboBox itemTypes = new JComboBox(new String[]{"Books","Dvds","Children's Books","VHS"});
        itemTypes.setMaximumSize(new Dimension(300,20));
        add(itemTypes);
        */
        m = new DefaultListModel();
        inventoryItems =  new JList(m);        
        inventoryItems.setCellRenderer(new MyListCellRenderer());        
        JScrollPane spInventory = new JScrollPane(inventoryItems);
        add(spInventory); 
        
        l.addDateChangeListeners(new InventoryChangeListener(){
            @Override
            public void onChange() {
                m.clear();
                if(overdueOnly){
                   for(Item i:l.getOverdueItems()){
                        m.addElement(i);
                    } 
                }else{
                    for(Item i:l.getAllItems()){
                        m.addElement(i);
                    }
                }
            }
        });
        
        l.addInventoryChangeListener(new InventoryChangeListener(){
            @Override
            public void onChange() {
                m.clear();
                if(overdueOnly){
                   for(Item i:l.getOverdueItems()){
                        m.addElement(i);
                    } 
                }else{
                    for(Item i:l.getAllItems()){
                        m.addElement(i);
                    }
                }
            }
        });
        l.fireUpdateListeners();
    }
    
    public void addItem(Item i){
        l.addItem(i);
    }  
    
    public Item getSelectedItem(){
        return (Item)inventoryItems.getSelectedValue();
    }
    
    class MyListCellRenderer implements ListCellRenderer{ 
        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            ItemPane item = new ItemPane((Item)value);
            if(index%2==0){
                item.setBackground(Color.WHITE);
            }
            if(cellHasFocus){
                item.setBackground(Color.BLUE);
            }
            if(item.getItem().isCheckedOut()){
                if(item.getItem().getDueDate().before(l.getDate())){
                    item.setBackground(Color.PINK);
                }
            }
            return item;       
        }
    }
}

